// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------


private alias Object.Value0 : Object.Timer

private alias object.value3 : object.postivecap
private alias object.value4 : object.negativecap

private alias object.value6 : player.invulnerable

private alias Object[40].Value11 : Player.Xpos


reserve function DeltaSoul_Movement
reserve function DeltaSoul_Blue

// State aliases
private alias 0  : DELTA_SOULSUMMON_0
private alias 1  : DELTA_SOULMOVE_1
private alias 2  : DELTA_SOUL_LEAVE_2
private alias 3  : DELTA_SOUL_DEAD_3
private alias 4  : DELTA_SOUL_SHATTER_4

public value Delta_HP = 90;
public value Delta_YourTurn = 1;
public value Delta_SlenderMercy = 0;

function DeltaSoul_Movement
	temp5 = 0

	SetBit(temp5, 0, object.up)
	SetBit(temp5, 1, object.down)
	SetBit(temp5, 2, object.left)
	SetBit(temp5, 3, object.right)

	if inputdown.buttonA == 1
		object.postivecap = 0x7500
		object.negativecap = -0x7500
	else
		object.postivecap = 0x16500
		object.negativecap = -0x16500
	end if

	temp6 = temp5
	temp6 &= 3
	switch temp6	
		case 1	//hold up
			if object.yvel >= object.negativecap
				object.yvel-= 0x5500
			else
				object.yvel = object.negativecap
			end if	
			break
		case 2	//hold down
			if object.yvel <= object.postivecap
				object.yvel+= 0x5500
			else
				object.yvel = object.postivecap
			end if	
		
			break
		default	//none
			object.yvel = 0
			break
	end switch
	
	temp6 = temp5
	temp6 &= 12
	switch temp6
		case 4	//left
			if object.xvel >= object.negativecap
				object.xvel-= 0x5500
			else
				object.xvel = object.negativecap
			end if	
			
			break
		case 8	//right
			if object.xvel <= object.postivecap
				object.xvel+= 0x5500
			else
				object.xvel = object.postivecap
			end if	
			
			break
		default
			object.xvel = 0
			break
	end switch
	object.speed = object.xvel
end function



event ObjectMain
	Object.Priority = PRIORITY_ACTIVE
	object.groupID = GROUP_PLAYERS
	object.value38 = HITBOX_AUTO
	object.value39 = HITBOX_AUTO
	object.value40 = HITBOX_AUTO
	object.value41 = HITBOX_AUTO

	switch object.state
	case DELTA_SOULSUMMON_0
		object.drawOrder = 6

		temp0 = screen.xcenter
		temp0 *= 0x10000
		if object.xpos < temp0
			Object.Xpos += 0x20000
		else
			Object.Xpos = temp0
		end if

		temp1 = 0x680000
		if Object.Ypos > temp1
			Object.Ypos -= 0x20000
		else
			Object.Ypos = temp1
		end if

		if object[40].state == 6
			object.state++
		end if

	break
	case DELTA_SOULMOVE_1
		if Delta_HP <= 0
			object.xvel = 0
			object.speed = 0
			object.yvel = 0
			object.visible = 0
			playsfx(SfxName[SoulShatter], 0)
			object.state = DELTA_SOUL_DEAD_3
		end if

		if player.invulnerable > 0
			player.invulnerable--
			GetBit(temp0, player.invulnerable, 2)
			if temp0 == 1
				object.visible = 0
			else
				object.visible = 1
			end if
		end if

		ProcessObjectControl()
		ProcessObjectMovement()
		Callfunction(DeltaSoul_Movement)

		if object[40].state == 7
			object.state++
		end if		
	break
	case DELTA_SOUL_LEAVE_2
		object.xpos += object.xvel
		object.ypos += object.yvel

		temp0 = Player.Xpos
		temp0 *= 0x12000
		if object.xpos > temp0
			object.xvel -= 0x4000
		else
			object.xpos = temp0
			object.xvel = 0
			object.type = TypeName[Blank Object]
		end if


		temp1 = screen.ycenter
		temp1 *= 0x10000
		if object.ypos > temp1
			object.yvel -= 0x4000
		else
			object.ypos = temp1
			object.yvel = 0
		end if
	break
	case DELTA_SOUL_DEAD_3
		object.visible = 1
		object.timer++
		if object.timer == 100
			object.state = DELTA_SOUL_SHATTER_4
			object.timer = 0
			Object.Frame = 1
			playsfx(SfxName[SoulDie], 0)
			CreateTempObject(TypeName[Delta Soul], 1, Object.Xpos, Object.Ypos)
			object[tempObjectPos].drawOrder = 6	
			object[tempObjectPos].state = 4
			CreateTempObject(TypeName[Delta Soul], 2, Object.Xpos, Object.Ypos)
			object[tempObjectPos].drawOrder = 6	
			object[tempObjectPos].state = 4			
			CreateTempObject(TypeName[Delta Soul], 3, Object.Xpos, Object.Ypos)
			object[tempObjectPos].drawOrder = 6	
			object[tempObjectPos].state = 4		
		end if
	break
	case DELTA_SOUL_SHATTER_4
		object.ypos += object.yvel 
		object.yvel += 2400
		object.xpos += object.xvel

		switch object.propertyValue
			case 1
				object.xvel += 0x200
			break
			case 2
				object.xvel -= 0x200
			break			
			case 3
				object.xvel -= 0x400
			break					
		end switch

		if object.outofbounds == 1
			object.type = TypeName[Blank Object]
		end if

		Object.Timer += 64
		
		if Object.Timer > 239
			Object.Timer -= 240
			
			Object.Frame++
			if Object.Frame > 4
				Object.Frame = 1
			end if
		end if

	break
end switch
end event

event ObjectDraw
//	DrawSpriteScreenXY(0, Object.Xpos, Object.Ypos)
	switch object.state
	case 3
		DrawSpriteXY(5, Object.Xpos, Object.ypos)
	break	
	case 4
		DrawSpriteXY(Object.Frame, Object.Xpos, Object.ypos)
	break
	default
		ProcessAnimation()
		DrawObjectAnimation()
	break
	end switch
end event

event ObjectStartup
	LoadSpriteSheet("3D_Test/DeltaSheet.gif")
	LoadAnimation("Soul.ani")
	SpriteFrame(-2, 0, 10, 10, 37, 100)	//Soul	0

	SpriteFrame(-2, 0, 8, 8, 330, 301)	//Shatter Piece 1
	SpriteFrame(-2, 0, 8, 8, 339, 301)	//Shatter Piece 2
	SpriteFrame(-2, 0, 8, 8, 348, 301)	//Shatter Piece 3
	SpriteFrame(-2, 0, 8, 8, 357, 301)	//Shatter Piece 4	

	SpriteFrame(-6, -5, 12, 10, 317, 299)	//S
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
